<?php

namespace Biz\model\store;

use Doraemon\pockets\datebase\BaseDbModel;
use Doraemon\tools\ImgUrl;

class StoreBasic extends BaseDbModel
{
    const _CONFIG_FILE_ = 'db_store';

    const _CONFIG_SELECT_ = 'db_store';

    const _DATABASE_ = 'store';

    const _TABLE_NAME = 'store_basic';

    //门店状态
    const STORE_STATUS_DEFAULT = 0;//默认
    const STORE_STATUS_NORMAL = 1;//有效

    //问答模块状态
    const ISSUE_ON = 1;
    const ISSUE_OFF = 2;

    /**
     * @param array $field
     * @return array
     */
    public static function getAll(array $field = []): array
    {
        if (!$field) {
            $field = '*';
        }
        $where['store_status'] = self::STORE_STATUS_NORMAL;
        $list = self::select($field, $where);
        $formatData = [];
        if ($list) {
            foreach ($list as $item) {
                $info = self::formatInfo($item);
                $formatData[$item["id"]] = $info;
            }
        }

        return $formatData;
    }

    /**
     * @param array $idArr
     * @param array $field
     * @return array
     */
    public static function getByIds(array $idArr, array $field = []): array
    {
        if (!$field) {
            $field = '*';
        }
        $where['id'] = $idArr;
        $where['store_status'] = self::STORE_STATUS_NORMAL;
        $list = self::select($field, $where);
        $formatData = [];
        if ($list) {
            foreach ($list as $item) {
                $info = self::formatInfo($item);
                $formatData[$item["id"]] = $info;
            }
        }
        return $formatData;
    }

    /**
     * @param $info
     * @return array
     */
    public static function formatInfo($info): array
    {
        foreach ($info as $key => $val) {
            switch ($key) {
                case 'logo_pic':
                    $info['logo_url'] = $val ? ImgUrl::getUrl(ImgUrl::APPTYPE_BIZ, $val,
                        ImgUrl::IMAGE_SIZE_ORI_NPHONE) : '';
                    break;
                case 'cover_pic':
                    $info['cover_pic_url'] = $val ? ImgUrl::getUrl(ImgUrl::APPTYPE_BIZ, $val,
                        ImgUrl::IMAGE_SIZE_ORI_NPHONE) : '';
                    break;
                case 'card_cover_pic':
                    $info['card_cover_pic_url'] = $val ? ImgUrl::getUrl(ImgUrl::APPTYPE_BIZ, $val,
                        ImgUrl::IMAGE_SIZE_ORI_NPHONE) : '';
                    break;
                default :
                    break;
            }

        }

        return $info;
    }

    /**
     * 获取单条单个字段
     * @param int $store_id
     * @param string $field
     * @return array|null
     */
    public static function getField(int $store_id, string $field)
    {
        $where['id'] = $store_id;
        return self::first($where, $field);
    }

    /**
     * 获取一行
     * @param $where
     * @param string $field
     * @return array|null
     */
    public static function first($where, $field = '*')
    {
        $rs = self::get($field, $where);
        if (!$rs) {
            return null;
        }

        return $rs;
    }

    /*添加basic信息
     * @param array $data
     * @return int $id
     * */
    public static function add($data)
    {
        $result = self::insert($data)->rowCount();
        if ($result) {
            return self::id();
        }

        return false;
    }

    /*修改basic信息
     *@param int $id
     * @param array $data
     * @return int $id
   * */
    public static function updateById($id, $data)
    {
        $result = self::update($data, ['id' => $id])->rowCount();
        if ($result) {
            return $id;
        }

        return false;
    }


    /*获取信息
       *@param int $id
       * @param array $data
       * @return int $id
   * */
    public static function getInfoByAid($account_id)
    {
        $result = self::get("*", ['account_id' => $account_id]);

        return $result;
    }

    /**
     * @param $storeId
     * @return array|mixed
     */
    public static function getStoreInfoById($storeId)
    {
        return self::get("*", [
            "AND" => [
                'id' => $storeId,
                "store_status" => self::STORE_STATUS_NORMAL,
            ],
        ]);
    }

    public static function getInfo($storeId)
    {
        return self::get("*", [
            "AND" => [
                'id' => $storeId,
            ],
        ]);
    }

    /**
     * @param $uid
     * @param string $field
     * @return array|mixed
     */
    public static function getStoreInfoByUid($uid, $field = '*')
    {
        return self::get($field, [
            "AND" => [
                'uid' => $uid,
                'store_status' => self::STORE_STATUS_NORMAL
            ],
        ]);
    }

    /**
     * @param $where
     * @param string $field
     * @return array|bool
     */
    public static function getStoreByFilter($where, $field = '*')
    {
        $where['store_status'] = self::STORE_STATUS_NORMAL;

        return self::select($field, $where);
    }

    public static function getStoreCountByWhere($where)
    {
        $where['store_status'] = self::STORE_STATUS_NORMAL;
        return self::count($where);
    }


    /*获取数据通过store_uids
     *
     * */
    public static function getStoreListByUids($uids, $fields = '*')
    {
        $list = self::select($fields, [
            'AND' => [
                'uid' => $uids,
                'store_status' => self::STORE_STATUS_NORMAL
            ]
        ]);
        $formatData = [];
        if ($list) {
            foreach ($list as $item) {
                $info = self::formatInfo($item);
                $formatData[$item["id"]] = $info;
            }
        }
        return $list;
    }

    // 获取所有正常门店信息
    public static function getAllNormalStores()
    {
        return self::select("*", [
            'AND' => [
                'store_status' => self::STORE_STATUS_NORMAL
            ]
        ]);
    }


}